import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/controllers/login_controller/login_page_controller.dart';
import 'package:xinxuan/controllers/my_controller/withdrawal_page_controller.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:get/get.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:xinxuan/vo/verification_vo.dart';

class WithdrawalDialog extends Dialog {

  final CommonPageController commonPageController = Get.find();
  final WithdrawalPageController withdrawalPageController = Get.find();
  final LoginPageController loginPageController = Get.put(LoginPageController());
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor:  Colors.transparent,//设置为透明色
      body: Center(
        child: Container(
          width: 320.w,
          decoration: BoxDecoration(
            color: bgWhiteColor,
            borderRadius: BorderRadius.circular(8.w)
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                padding: EdgeInsets.only(left: 12.w, right: 12.w, top: 16.h),
                child: Column(
                  children: [
                    Container(
                      padding: EdgeInsets.only(bottom: 6.h),
                      alignment: Alignment.center,
                      child: Text(
                        '提现',
                        style: TextStyle(
                          fontSize: 20.sp,
                          color: textBlackColor
                        )
                      ),
                    ),
                    Container(
                      height: 44.h,
                      margin: EdgeInsets.only(top: 12.h),
                      padding: EdgeInsets.symmetric(horizontal: 12.w),
                      decoration: BoxDecoration(
                        border: Border.all(
                          width: .5,
                          color: lineColor
                        ),
                        borderRadius: BorderRadius.circular(5.w)
                      ),
                      child: TextField(
                        decoration: InputDecoration(
                          border: OutlineInputBorder(borderSide: BorderSide.none),
                          contentPadding: EdgeInsets.zero,
                          counterText: '',
                          hintText: '请输入提现金额',
                          hintStyle: TextStyle(
                            fontSize: 14.sp,
                            color: textSubtitleColor,
                          )
                        ),
                        cursorColor: textBlackColor,
                        keyboardType: TextInputType.number,
                        inputFormatters: [
                          FilteringTextInputFormatter.allow(RegExp('[0-9]')),
                        ],
                        style: TextStyle(
                          fontSize: 14.sp,
                          color: textBlackColor
                        ),
                        onChanged: (String value) {
                          withdrawalPageController.amount = value;
                        },
                      ),
                    ),
                    Obx(() => Column(
                      children: [
                        _item(withdrawalPageController.selectedBankItem.value.id == 0 ? '请输入姓名' : withdrawalPageController.selectedBankItem.value.accountName!),
                        _item(withdrawalPageController.selectedBankItem.value.id == 0 ? '请输入身份证号' : withdrawalPageController.selectedBankItem.value.identityNumber!),
                        _item(withdrawalPageController.selectedBankItem.value.id == 0 ? '请选择银行卡' : '${withdrawalPageController.selectedBankItem.value.accountName} / ${withdrawalPageController.selectedBankItem.value.bank!.title} / ${withdrawalPageController.selectedBankItem.value.cardNumber}'),
                      ],
                    )),
                    Container(
                      height: 44.h,
                      margin: EdgeInsets.only(top: 12.h),
                      padding: EdgeInsets.symmetric(horizontal: 12.w),
                      decoration: BoxDecoration(
                        border: Border.all(
                          width: .5,
                          color: lineColor
                        ),
                        borderRadius: BorderRadius.circular(5.w)
                      ),
                      child: Row(
                        children: [
                          Expanded(child: TextField(
                            decoration: InputDecoration(
                              border: OutlineInputBorder(borderSide: BorderSide.none),
                              contentPadding: EdgeInsets.zero,
                              counterText: '',
                              hintText: '请输入验证码',
                              hintStyle: TextStyle(
                                fontSize: 14.sp,
                                color: textSubtitleColor,
                              )
                            ),
                            cursorColor: textBlackColor,
                            keyboardType: TextInputType.number,
                            inputFormatters: [
                              FilteringTextInputFormatter.allow(RegExp('[0-9]')),
                            ],
                            maxLength: 6,
                            style: TextStyle(
                              fontSize: 14.sp,
                              color: textBlackColor
                            ),
                            onChanged: (String value) {
                              withdrawalPageController.verificationCode = value;
                            },
                          )),
                          Obx(() => loginPageController.duration.value >= loginPageController.totalDuration ? Container(
                            width: 90.w,
                            child: ButtonUtil(
                              text: '获取验证码', 
                              fontSize: 14.sp, 
                              borderRadius: 30.w, 
                              height: 30.h, 
                              onTop: () async {
                                if (loginPageController.duration.value >= loginPageController.totalDuration) {
                                  loginPageController.mobile.value = commonPageController.userData.value.mobile!;
                                  VerificationVo? res = await loginPageController.getSmsCode('withdraw');
                                  if (res != null) {
                                    withdrawalPageController.verificationKey = res.data!.verificationKey!;
                                  }
                                }
                              }
                            ),
                          ) : Container(
                            width: 60.w,
                            child: ButtonUtil(
                              text: '${loginPageController.duration.value}s', 
                              textColor: textSubtitleColor,
                              fontSize: 14.sp, 
                              borderRadius: 30.w, 
                              height: 30.h, 
                              bgColors: [
                                lineColor,
                                lineColor,
                              ],
                              onTop: () {}
                            ),
                          ))
                        ],
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.symmetric(vertical: 12.h),
                      child: Text.rich(
                        TextSpan(
                          text: '本次最低提现',
                          style: TextStyle(
                            color: textBlackColor,
                            fontSize: 12.sp
                          ),
                          children: [
                            TextSpan(
                              text: '200',
                              style: TextStyle(
                                color: mainTextColor,
                                fontSize: 12.sp
                              ),
                            ),
                            TextSpan(
                              text: '元',
                              style: TextStyle(
                                color: textBlackColor,
                                fontSize: 12.sp
                              ),
                            )
                          ]
                        )
                      ),
                    )
                  ],
                ),
              ),
              Container(
                height: 56.h,
                decoration: BoxDecoration(
                  border: Border(
                    top: BorderSide(
                      width: .5.h,
                      color: lineColor
                    )
                  )
                ),
                child: Row(
                  children: [
                    InkWellUtil.shakeProofInkWell(Container(
                      width: 160.w,
                      height: 56.h,
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        color: Colors.transparent,
                        border: Border(
                          right: BorderSide(
                            color: lineColor,
                            width: .5
                          )
                        )
                      ),
                      child: Text(
                        '取消',
                        style: TextStyle(
                          fontSize: 16.sp,
                          color: textSubtitleColor
                        ),
                      ),
                    ), () async {
                      Get.back();
                    }),
                    InkWellUtil.shakeProofInkWell(Container(
                      width: 159.w,
                      height: 56.h,
                      alignment: Alignment.center,
                      color: Colors.transparent,
                      child: Text(
                        '确定',
                        style: TextStyle(
                          fontSize: 16.sp,
                          color: mainTextColor
                        ),
                      ),
                    ), () async {
                      withdrawalPageController.withdrawal();
                    })
                  ],
                )
              )
            ],
          ),
        ),
      ),
    );
  }

  /// 
  Widget _item(String subTitle) {
    return InkWellUtil.shakeProofInkWell(Container(
      constraints: BoxConstraints(
        minHeight: 44.h
      ),
      margin: EdgeInsets.only(top: 12.h),
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      alignment: Alignment.centerLeft,
      decoration: BoxDecoration(
        border: Border.all(
          width: .5,
          color: lineColor
        ),
        borderRadius: BorderRadius.circular(5.w)
      ),
      child: Row(
        children: [
          Expanded(child: Text(
            subTitle,
            overflow: TextOverflow.ellipsis,
            maxLines: 3,
            style: TextStyle(
              color: withdrawalPageController.selectedBankItem.value.id! == 0 ? textSubtitleColor : textBlackColor,
              fontSize: 14.sp
            ),
          )),
          Image.asset(
            'assets/icons/icon_arrow_right.png',
            width: 15.w,
            height: 15.w,
          )
        ],
      ),
    ), () {
      Get.bottomSheet(Container(
        height: 300.h,
        decoration: BoxDecoration(
          color: bgWhiteColor,
          borderRadius: BorderRadius.vertical(top: Radius.circular(5.w))
        ),
        child: Column(
          children: [
            Container(
              height: 56.h,
              padding: EdgeInsets.symmetric(horizontal: 16.w),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  InkWellUtil.shakeProofInkWell(Text(
                    '取消',
                    style: TextStyle(
                      color: textSubtitleColor,
                      fontSize: 16.sp
                    ),
                  ), () {
                    Get.back();
                  }),
                  InkWellUtil.shakeProofInkWell(Text(
                    '确认',
                    style: TextStyle(
                      color: mainTextColor,
                      fontSize: 16.sp
                    ),
                  ), () {
                    withdrawalPageController.determineBank();
                    Get.back();
                  })
                ],
              ),
            ),
            Expanded(
              child: ScrollConfigurationCommon(child: CupertinoPicker(
                scrollController: withdrawalPageController.scrollController.value,
                diameterRatio: 5.w,
                itemExtent: 54.h, 
                onSelectedItemChanged: (int index) {
                  withdrawalPageController.changeBankIndex(index);
                }, 
                children: withdrawalPageController.bankCardList.map((item) {
                  return Container(
                    height: 54.h,
                    width: 375.w,
                    alignment: Alignment.center,
                    child: Text(
                      '${item.accountName} / ${item.bank!.title} / ${item.cardNumber}',
                      overflow: TextOverflow.ellipsis,
                      maxLines: 2,
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        color: textBlackColor,
                        fontSize: 16.sp
                      ),
                    ),
                  );
                }).toList()
              )),
            )
          ],
        ),
      ));
    });
  }
}